function oil = osl_check_oil(oilin)

% oil = osl_check_oil(oil)
%
% Checks an OIL (OHBA's ICA pipeLine) struct has all the appropriate 
% settings, which can then be passed to osl_run_oil to do an OIL 
% analysis. Throws an error if any required inputs are missing, fills other 
% settings with default values.
%
% OIL can analysis task-positive and resting state data. However, the user
% must specify the type of analysis using:
%
% oil.paradigm = 'task'; OR oil.paradigm = 'rest';
%
% OIL has six stages with the following required inputs
%
% For oil.source_recon:
%
% oil.source_recon.conditions; list of conditions to include in the
% analysis, e.g. oil.source_recon.conditions={'Motorbike','Neutral face'}; 
% oil.source_recon.D_continuous; list of continuous time SPM MEEG object
% file paths to run the analysis on (list order should correspond to
% oil.source_recon.mri and oil.source_recon.D_epoched fields if provided) 
% AND/OR
% oil.source_recon.D_epoched; list of epoched SPM MEEG object file paths to
% run the analysis on (list order should correspond to oil.source_recon.mri
% and oil.source_recon.D_epoched fields if provided) 
%
% e.g. oil.source_recon.D_epoched{1}='subject1'; oil.source_recon.D_epoched{2}='subject2'
% oil.source_recon.time_range; Time range (from to) within trial, in secs,
% need to all be the same duration and one for each condition.   
% oil.source_recon.freq_range; Frequency range (from to) in Hz
%
% For oil.enveloping:
%
% NONE
%
% For oil.concat_subs:
%
% NONE
%
% For oil.ica:
%
% NONE
%
% For oil.ica_first_level:
%
% oil.ica_first_level.design_matrix_summary & oil.ica.contrast are required
% to run first level statistics on ICA outputs. See osl_example_oil.m for
% example inputs.
%
% For oil.ica_group_level:
%
% NONE
%
% Optional inputs:
%
% See inside this function (e.g. use "type osl_check_oil") to see the other
% optional settings
%
% HL 250313
%
% Version 1.1
%
% henry.luckhoo@trinity.ox.ac.uk

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% onverall settings:
try, oil.to_do    = oilin.to_do;    oilin = rmfield(oilin,'to_do');     catch, oil.to_do=[1 1 1 1 0 0];                                                                            end;
try, oil.do_plots = oilin.do_plots; oilin = rmfield(oilin,'do_plots');  catch, oil.do_plots=0;                                                                                     end;
try, oil.paradigm = oilin.paradigm; oilin = rmfield(oilin,'paradigm');  catch, error('User must specify the paradigm type: oil.paradigm, It can be either ''rest'' or ''task''.'); end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% source recon settings
oil.source_recon=[]; 

% required settings:
try, oil.source_recon.conditions = oilin.source_recon.conditions; oilin.source_recon = rmfield(oilin.source_recon,'conditions'); catch, error('oilin.source_recon.conditions not specified, list of conditions (conditions) to include in the analysis'); end; % e.g. conditions={'Motorbike','Neutral face');
try, oil.source_recon.time_range = oilin.source_recon.time_range; oilin.source_recon = rmfield(oilin.source_recon,'time_range'); catch, error('oilin.source_recon.time_range not specified'); end; % Time range (from to) within trial, in secs, need to all be the same duration and one for each condition    
try, oil.source_recon.freq_range = oilin.source_recon.freq_range; oilin.source_recon = rmfield(oilin.source_recon,'freq_range'); catch, error('oilin.source_recon.freq_range not specified'); end; % Frequency range (from to) in Hz

try, oil.source_recon.D_continuous=oilin.source_recon.D_continuous; oilin.source_recon = rmfield(oilin.source_recon,'D_continuous'); catch, oil.source_recon.D_continuous=[]; end; % list of continuous SPM MEEG object file paths to run the analysis on (list order should correspond to oil.source_recon.mri and oil.source_recon.D_epoched fields if provided)
% AND/OR:
try, oil.source_recon.D_epoched=oilin.source_recon.D_epoched; oilin.source_recon = rmfield(oilin.source_recon,'D_epoched'); catch, oil.source_recon.D_epoched=[]; end; % list of epoched SPM MEEG object file paths - this will be used to obtain the bad channel and bad trial info. (list order should correspond to oil.source_recon.mri and oil.source_recon.D_continuous fields if provided)

% check list of SPM MEEG filenames input
if(~isempty(oil.source_recon.D_epoched)),
    Ds=oil.source_recon.D_epoched;
elseif(~isempty(oil.source_recon.D_continuous)),
    Ds=oil.source_recon.D_continuous;
else
    error('Either oil.source_recon.D_continuous, or oil.source_recon.D_epoched need to be specified');   
end;
num_subjects=length(Ds);

chant='unknown';

% auto detect datatype so that defaults can be set intelligently
D=spm_eeg_load(Ds{1});
chant=D.chantype;
if sum(ismember(unique(chant),'MEGGRAD')), 
    disp('Detected CTF data. Using default CTF settings');  
    datatype='ctf';
elseif sum(ismember(unique(chant),'MEGPLANAR')) && ~sum(ismember(unique(chant),'MEGPCACOMP'))
    disp('Detected Elekta Neuromag 306 data. Using default Elekta Neuromag 306 settings.'); 
    datatype='neuromag';
elseif sum(ismember(unique(chant),'EEG')), 
    disp('Detected EEG data. Using default EEG settings');  
    datatype='eeg';
else
    disp('Unknown datatype. Using default Elekta Neuromag 306 settings');  
    datatype='neuromag';    
end

% optional settings:
try, oil.source_recon.dirname              = oilin.source_recon.dirname;               oilin.source_recon = rmfield(oilin.source_recon,'dirname');               catch, oil.source_recon.dirname=[Ds{1} '.oil'];                                                                                                                               end; % directory name which will be created and within which all results associated with this source recon will be stored
try, oil.source_recon.sessions_to_do       = oilin.source_recon.sessions_to_do;        oilin.source_recon = rmfield(oilin.source_recon,'sessions_to_do');        catch, oil.source_recon.sessions_to_do=1:num_subjects;                                                                                                                        end; % list of subject indexes to run source_recon on.
try, oil.source_recon.epochinfo            = oilin.source_recon.epochinfo;             oilin.source_recon = rmfield(oilin.source_recon,'epochinfo');             catch, oil.source_recon.epochinfo=[];                                                                                                                                         end; % epochinfo for passing to spm_eeg_epoch (see help spm_eeg_epoch). if empty, will look inside D_epoched for D_epoched.epochinfo
try, oil.source_recon.method               = oilin.source_recon.method;                oilin.source_recon = rmfield(oilin.source_recon,'method');                catch, oil.source_recon.method='beamform';                                                                                                                                    end; % can be 'beamform' or 'none' (for a sensor space analysis)

try, oil.source_recon.modalities=oilin.source_recon.modalities; oilin.source_recon = rmfield(oilin.source_recon,'modalities'); 
catch, 
    switch datatype
        case 'neuromag'
            oil.source_recon.modalities={'MEGPLANAR', 'MEGMAG'}; 
        case 'ctf' 
            oil.source_recon.modalities={'MEG'}; 
        case 'eeg' 
            oil.source_recon.modalities={'EEG'}; 
    end;
end; % modalities to include

try, oil.source_recon.normalise_method      = oilin.source_recon.normalise_method;      oilin.source_recon = rmfield(oilin.source_recon,'normalise_method');      catch, oil.source_recon.normalise_method = 'min_eig';              end; % normalisation method to normalise the different channel modalities. Either 'min_eig' (recommended) or 'mean_eig' or 'none' (not recommended)
try, oil.source_recon.pca_dim               = oilin.source_recon.pca_dim;               oilin.source_recon = rmfield(oilin.source_recon,'pca_dim');               catch, oil.source_recon.pca_dim=60;                                end; % rank to be use for the pca dimensionality reduction. If pca_dim=-1 then spm_pca_order is used to estimate pca_dim. If oil.source_recon.do_reduce_pca=1 then this is used prior to any source recon, if oil.source_recon.do_reduce_pca=0 then this is only used to determine the rank of the beamformer data covariance
try, oil.source_recon.regpc                 = oilin.source_recon.regpc;                 oilin.source_recon = rmfield(oilin.source_recon,'regpc');                 catch, oil.source_recon.regpc=0;                                   end; % data covariance regularisation
try, oil.source_recon.gridstep              = oilin.source_recon.gridstep;              oilin.source_recon = rmfield(oilin.source_recon,'gridstep');              catch, oil.source_recon.gridstep=7;                                end; % space between dipoles in beamformer grid in mm 
try, oil.source_recon.mask_fname            = oilin.source_recon.mask_fname;            oilin.source_recon = rmfield(oilin.source_recon,'mask_fname');            catch,                                                             end;
try, oil.source_recon.useheadshape          = oilin.source_recon.useheadshape;          oilin.source_recon = rmfield(oilin.source_recon,'useheadshape');          catch, oil.source_recon.useheadshape=1;                            end;
try, oil.source_recon.forward_meg           = oilin.source_recon.forward_meg;           oilin.source_recon = rmfield(oilin.source_recon,'forward_meg');           catch, oil.source_recon.forward_meg='MEG Local Spheres';           end; % MEG head forward model set to 'Single Shell' or 'MEG Local Spheres'
try, oil.source_recon.beamformer_fn         = oilin.source_recon.beamformer_fn;         oilin.source_recon = rmfield(oilin.source_recon,'beamformer_fn');         catch, oil.source_recon.beamformer_fn='osl_beamformer_spm_batch';  end;
try, oil.source_recon.mri                   = oilin.source_recon.mri;                   oilin.source_recon = rmfield(oilin.source_recon,'mri');                   catch, for i=1:length(Ds), oil.source_recon.mri{i}=''; end;        end;                      
try, oil.source_recon.work_in_pca_subspace  = oilin.source_recon.work_in_pca_subspace;  oilin.source_recon = rmfield(oilin.source_recon,'work_in_pca_subspace');  catch, oil.source_recon.work_in_pca_subspace=1;                    end; % Do beamforming in a reduced PCA subspace (with dim determined by pca_dim)
try, oil.source_recon.force_pca_dim         = oilin.source_recon.force_pca_dim;         oilin.source_recon = rmfield(oilin.source_recon,'force_pca_dim');         catch, oil.source_recon.force_pca_dim=0;                           end; % Force the beamformer to use the initial pca_dim rather than attempting to correct it.
try, oil.source_recon.mni_coords            = oilin.source_recon.mni_coords;            oilin.source_recon = rmfield(oilin.source_recon,'mni_coords');            catch,                                                             end; % num_coords x 3
try, oil.source_recon.type                  = oilin.source_recon.type;                  oilin.source_recon = rmfield(oilin.source_recon,'type');                  catch, oil.source_recon.type='scalar';                             end; % 'scalar' or 'vector'
try, oil.source_recon.bandstop_filter_mains = oilin.source_recon.bandstop_filter_mains; oilin.source_recon = rmfield(oilin.source_recon,'bandstop_filter_mains'); catch, oil.source_recon.bandstop_filter_mains = 0;                 end % defaults to applying a bandstop filter at 50Hz and higher harmonics


try 
    oil.source_recon.fid_label = oilin.source_recon.fid_label; 
    oilin.source_recon         = rmfield(oilin.source_recon,'fid_label'); 
catch 
    switch datatype
        case 'neuromag'
            disp('Using default Neuromag 306 fid labels\n');
            oil.source_recon.fid_label.nasion = 'Nasion'; 
            oil.source_recon.fid_label.lpa    = 'LPA'; 
            oil.source_recon.fid_label.rpa    = 'RPA'; 
        case 'ctf' 
            disp('Using default ctf fid labels\n');
            oil.source_recon.fid_label.nasion = 'nas'; 
            oil.source_recon.fid_label.lpa    = 'lpa'; 
            oil.source_recon.fid_label.rpa    = 'rpa'; 
        case 'eeg' 
            disp('Using default eeg fid labels\n');
            oil.source_recon.fid_label.nasion = 'Nasion'; 
            oil.source_recon.fid_label.lpa    = 'LPA'; 
            oil.source_recon.fid_label.rpa    = 'RPA'; 
    end;
end; % To see what these should be look at: D.fiducials.fid.label
  
try 
    oil.source_recon.neuromag_planar_baseline_correction = oilin.source_recon.neuromag_planar_baseline_correction; 
    oilin.source_recon = rmfield(oilin.source_recon,'neuromag_planar_baseline_correction'); 
    
catch
    switch datatype
        case 'neuromag'
            warning(['Using Neuromag Vector View defaults ', ...
                     '(e.g. this may need changing if you are using ', ...
                     'a Triux system)\n']); 
            oil.source_recon.neuromag_planar_baseline_correction='vector_view'; 
        otherwise
            disp('Using no planar baseline correction by default');
            oil.source_recon.neuromag_planar_baseline_correction='none';
    end;
end; % e.g. 'none' for none or 'vector_view' vector_view correction, or 'triux'

% eyeblink artefacts
try, oil.source_recon.artefact_chanlabel = oilin.source_recon.artefact_chanlabel; oilin.source_recon = rmfield(oilin.source_recon,'artefact_chanlabel'); catch, oil.source_recon.artefact_chanlabel = []; end % defaults to no channel artefacts - empty array
% HMM optional settings - Currently Unspported in OIL
%try, oil.source_recon.hmm_num_states = oilin.source_recon.hmm_num_states; oilin.source_recon = rmfield(oilin.source_recon,'hmm_num_states'); catch, oil.source_recon.hmm_num_states=1; end; % -1 indicates not to do any HMM stuff
%try, oil.source_recon.hmm_num_starts = oilin.source_recon.hmm_num_starts; oilin.source_recon = rmfield(oilin.source_recon,'hmm_num_starts'); catch, oil.source_recon.hmm_num_starts=4; end; 
%try, oil.source_recon.hmm_pca_dim = oilin.source_recon.hmm_pca_dim; oilin.source_recon = rmfield(oilin.source_recon,'hmm_pca_dim'); catch, oil.source_recon.hmm_pca_dim=30; end; 
%try, oil.source_recon.hmm_block = oilin.source_recon.hmm_block; oilin.source_recon = rmfield(oilin.source_recon,'hmm_block'); catch, end; 
%try, oil.source_recon.hmm_av_class_occupancy=oilin.source_recon.hmm_av_class_occupancy; oilin.source_recon = rmfield(oilin.source_recon,'hmm_av_class_occupancy');  catch, oil.source_recon.hmm_av_class_occupancy=20; end; % do epoching after all preparation (e.g. after channel normalisation and PCA reduction) otherwise does it after just temporal filtering

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Enveloping Settings 

oil.enveloping=[]; if ~isfield(oilin,'enveloping'); oilin.enveloping = struct; end
try, oil.enveloping.window_length            = oilin.enveloping.window_length;            oilin.enveloping = rmfield(oilin.enveloping,'window_length');            catch, oil.enveloping.window_length             = 1;                                                                                           end;    
try, oil.enveloping.overlap                  = oilin.enveloping.overlap;                  oilin.enveloping = rmfield(oilin.enveloping,'overlap');                  catch, oil.enveloping.overlap                   = 0;                                                                                           end;
try, oil.enveloping.spatial_smoothing        = oilin.enveloping.spatial_smoothing;        oilin.enveloping = rmfield(oilin.enveloping,'spatial_smoothing');        catch, oil.enveloping.spatial_smoothing         = 4;                                                                                           end;
try, oil.enveloping.gridstep                 = oilin.enveloping.gridstep;                 oilin.enveloping = rmfield(oilin.enveloping,'gridstep');                 catch, oil.enveloping.gridstep                  = 8;                                                                                           end;
try, oil.enveloping.timewindow               = oilin.enveloping.timewindow;               oilin.enveloping = rmfield(oilin.enveloping,'timewindow');               catch, oil.enveloping.timewindow                = 'all';                                                                                       end;
try, oil.enveloping.name                     = oilin.enveloping.name;                     oilin.enveloping = rmfield(oilin.enveloping,'name');                     catch, oil.enveloping.name                      = [num2str(oil.enveloping.window_length) 's_win_' num2str(oil.enveloping.gridstep) 'mm_grid']; end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Concatenation Settings 

oil.concat_subs=[]; if ~isfield(oilin,'concat_subs'); oilin.concat_subs = struct; end
try, oil.concat_subs.sessions_to_do     = oilin.concat_subs.sessions_to_do;      oilin.concat_subs = rmfield(oilin.concat_subs,'sessions_to_do');      catch, oil.concat_subs.sessions_to_do = oil.source_recon.sessions_to_do;  end;
try, oil.concat_subs.normalise_subjects = oilin.concat_subs.normalise_subjects;  oilin.concat_subs = rmfield(oilin.concat_subs,'normalise_subjects');  catch, oil.concat_subs.normalise_subjects  = 0;                           end;
try, oil.concat_subs.demean_vox         = oilin.concat_subs.demean_vox;          oilin.concat_subs = rmfield(oilin.concat_subs,'demean_vox');          catch, oil.concat_subs.demean_vox          = 1;                           end;
try, oil.concat_subs.name               = oilin.concat_subs.name;                oilin.concat_subs = rmfield(oilin.concat_subs,'name');                catch, oil.concat_subs.name                = 'all_subs';                  end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  ICA Settings 

oil.ica=[]; if ~isfield(oilin,'ica'); oilin.ica = struct; end
try, oil.ica.temp_or_spat       = oilin.ica.temp_or_spat;        oilin.ica = rmfield(oilin.ica,'temp_or_spat');        catch, oil.ica.temp_or_spat  = 'temporal';                                                                                          end;
try, oil.ica.use_gm_mask        = oilin.ica.use_gm_mask;         oilin.ica = rmfield(oilin.ica,'use_gm_mask');         catch, oil.ica.use_gm_mask   = 0;                                                                                                   end;
try, oil.ica.num_ics            = oilin.ica.num_ics;             oilin.ica = rmfield(oilin.ica,'num_ics');             catch, oil.ica.num_ics       = 25;                                                                                                  end;
try, oil.ica.icasso_its         = oilin.ica.icasso_its;          oilin.ica = rmfield(oilin.ica,'icasso_its');          catch, oil.ica.icasso_its    = 0;                                                                                                   end;
try, oil.ica.last_eig           = oilin.ica.last_eig;            oilin.ica = rmfield(oilin.ica,'last_eig');            catch, oil.ica.last_eig      = oil.ica.num_ics;                                                                                     end;
try, oil.ica.other_oils         = oilin.ica.other_oils;          oilin.ica = rmfield(oilin.ica,'other_oils');          catch,                                                                                                                              end;
try, oil.ica.nuisance_oil_names = oilin.ica.nuisance_oil_names;  oilin.ica = rmfield(oilin.ica,'nuisance_oil_names');  catch,                                                                                                                              end;
try, oil.ica.normalise_vox      = oilin.ica.normalise_vox;       oilin.ica = rmfield(oilin.ica,'normalise_vox');       catch, oil.ica.normalise_vox = 0;                                                                                                   end;
try, oil.ica.initGuess          = oilin.ica.initGuess;           oilin.ica = rmfield(oilin.ica,'initGuess');           catch, oil.ica.initGuess = 0;                                                                                                       end;
try, oil.ica.name               = oilin.ica.name;                oilin.ica = rmfield(oilin.ica,'name');                catch, oil.ica.name          = [oil.ica.temp_or_spat '_ica_' num2str(oil.ica.num_ics) 'comps_' num2str(oil.ica.icasso_its) '_its']; end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  First Level ICA Stats Settings 

oil.ica_first_level=[]; if ~isfield(oilin,'ica_first_level'); oilin.ica_first_level = struct; end
% required settings:
if strcmp(oil.paradigm,'task')
try, oil.ica_first_level.design_matrix_summary = oilin.ica_first_level.design_matrix_summary; oilin.ica_first_level = rmfield(oilin.ica_first_level,'design_matrix_summary'); catch, error('design_matrix_summary not specified');                     end;
try, oil.ica_first_level.contrast              = oilin.ica_first_level.contrast;              oilin.ica_first_level = rmfield(oilin.ica_first_level,'contrast');              catch, error('contrast not specified');                                  end;
end
% optional settings
try, oil.ica_first_level.name                  = oilin.ica_first_level.name;                  oilin.ica_first_level = rmfield(oilin.ica_first_level,'name');                  catch, oil.ica_first_level.name           = 'ica_first_level';           end;
try, oil.ica_first_level.time_range            = oilin.ica_first_level.time_range;            oilin.ica_first_level = rmfield(oilin.ica_first_level,'time_range');            catch, oil.ica_first_level.time_range     = oil.source_recon.time_range; end;
try, oil.ica_first_level.use_robust_glm        = oilin.ica_first_level.use_robust_glm;        oilin.ica_first_level = rmfield(oilin.ica_first_level,'use_robust_glm');        catch, oil.ica_first_level.use_robust_glm = 0;                           end; % do robust GLM (uses bisquare via Matlab robustfit fn)
try, oil.ica_first_level.cope_type             = oilin.ica_first_level.cope_type;             oilin.ica_first_level = rmfield(oilin.ica_first_level,'cope_type');             catch, oil.ica_first_level.cope_type      = 'cope';                      end; % cope type to input to group GLM (from first level), set to 'coape', 'cope', or 'acope'
try, oil.ica_first_level.spatial_basis_set     = oilin.ica_first_level.spatial_basis_set;     oilin.ica_first_level = rmfield(oilin.ica_first_level,'spatial_basis_set');     catch,                                                                   end; % Spatial Basis Set from alternative analysis e.g. weights-normalised MEG or fMRI.
try, oil.ica_first_level.use_nonorm_data       = oilin.ica_first_level.use_nonorm_data;       oilin.ica_first_level = rmfield(oilin.ica_first_level,'use_nonorm_data');       catch, oil.ica_first_level.use_nonorm_data = 1;                          end; % To get single subject masks if needed for parcellation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Group Level ICA Stats Settings 

oil.ica_group_level=[]; if ~isfield(oilin,'ica_group_level'); oilin.ica_group_level = struct; end
try, oil.ica_group_level.group_design_matrix               = oilin.ica_group_level.group_design_matrix;               oilin.ica_group_level = rmfield(oilin.ica_group_level,'group_design_matrix');               catch, oil.ica_group_level.group_design_matrix               = ones(length(oil.concat_subs.sessions_to_do),1); end;
try, oil.ica_group_level.group_contrast                    = oilin.ica_group_level.group_contrast;                    oilin.ica_group_level = rmfield(oilin.ica_group_level,'group_contrast');                    catch, oil.ica_group_level.group_contrast{1}                 = [1];                                            end;
try, oil.ica_group_level.use_randomise                     = oilin.ica_group_level.use_randomise;                     oilin.ica_group_level = rmfield(oilin.ica_group_level,'use_randomise');                     catch, oil.ica_group_level.use_randomise                     = 0;                                              end;
try, oil.ica_group_level.Npermutations                     = oilin.ica_group_level.Npermutations;                     oilin.ica_group_level = rmfield(oilin.ica_group_level,'Npermutations');                     catch, oil.ica_group_level.Npermutations                     = 5000;                                           end;
try, oil.ica_group_level.name                              = oilin.ica_group_level.name;                              oilin.ica_group_level = rmfield(oilin.ica_group_level,'name');                              catch, oil.ica_group_level.name                              = 'ica_group_level';                              end;
try, oil.ica_group_level.demean_designmatrix               = oilin.ica_group_level.demean_designmatrix;               oilin.ica_group_level = rmfield(oilin.ica_group_level,'demean_designmatrix');               catch, oil.ica_group_level.demean_designmatrix               = 0;                                              end;
try, oil.ica_group_level.group_varcope_spatial_smooth_fwhm = oilin.ica_group_level.group_varcope_spatial_smooth_fwhm; oilin.ica_group_level = rmfield(oilin.ica_group_level,'group_varcope_spatial_smooth_fwhm'); catch, oil.ica_group_level.group_varcope_spatial_smooth_fwhm = 0;                                              end;
try, oil.ica_group_level.cluster_threshold                 = oilin.ica_group_level.cluster_threshold;                 oilin.ica_group_level = rmfield(oilin.ica_group_level,'cluster_threshold');                 catch, oil.ica_group_level.cluster_threshold                 = 2.3;                                            end;
try, oil.ica_group_level.comps2use                         = oilin.ica_group_level.comps2use;                         oilin.ica_group_level = rmfield(oilin.ica_group_level,'comps2use');                         catch, oil.ica_group_level.comps2use                         = 1:oil.ica.num_ics;                              end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% copy any results
try, oil.results                     = oilin.results;                     oilin                 = rmfield(oilin,'results');                                          catch, end;
% Source Recon Results
try, oil.source_recon.results_fnames = oilin.source_recon.results_fnames; oilin.source_recon    = rmfield(oilin.source_recon,'results_fnames');                      catch, end;
% Enveloping Results
try, oil.enveloping.results          = oilin.enveloping.results;          oilin.enveloping      = rmfield(oilin.enveloping,'results');                               catch, end;
% Concatenation Results
try, oil.concat_subs.results         = oilin.concat_subs.results;         oilin.concat_subs     = rmfield(oilin.concat_subs,'results');                              catch, end;
% ICA Results
try, oil.ica.results                 = oilin.ica.results;                 oilin.ica             = rmfield(oilin.ica,'results');                                      catch, end;
% First-level Results
try, oil.ica_first_level.results     = oilin.ica_first_level.results;     oilin.ica_first_level = rmfield(oilin.ica_first_level,'results');                          catch, end;
% Group-level Results
try, oil.ica_group_level.results     = oilin.ica_group_level.results;     oilin.ica_group_level = rmfield(oilin.ica_group_level,'results');                          catch, end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% check people haven't set any weird fields

weirdfields = fieldnames(oilin.source_recon);
if ~isempty(weirdfields)
    displayedWeirdFields = [];
    for iprint = 1:numel(weirdfields)
        displayedWeirdFields = [displayedWeirdFields weirdfields{iprint} ' '];
    end
    error([mfilename ':UnrecognisedFields'], ...
          ['The following oil.source_recon settings were not recognized ', ...
           'by osl_check_oil: \n ' displayedWeirdFields '\n']);
end % if ~isempty(weirdfields)

weirdfields = fieldnames(oilin.enveloping);
if ~isempty(weirdfields)
    displayedWeirdFields = [];
    for iprint = 1:numel(weirdfields)
        displayedWeirdFields = [displayedWeirdFields weirdfields{iprint} ' '];
    end
    error([mfilename ':UnrecognisedFields'], ...
          ['The following oil.enveloping settings were not recognized ', ...
           'by osl_check_oil: \n ' displayedWeirdFields '\n']);
end % if ~isempty(weirdfields)

weirdfields = fieldnames(oilin.concat_subs);
if ~isempty(weirdfields)
    displayedWeirdFields = [];
    for iprint = 1:numel(weirdfields)
        displayedWeirdFields = [displayedWeirdFields weirdfields{iprint} ' '];
    end
    error([mfilename ':UnrecognisedFields'], ...
          ['The following oil.concat_subs settings were not recognized ', ...
           'by osl_check_oil: \n ' displayedWeirdFields '\n']);
end % if ~isempty(weirdfields)

weirdfields = fieldnames(oilin.ica);
if ~isempty(weirdfields)
    displayedWeirdFields = [];
    for iprint = 1:numel(weirdfields)
        displayedWeirdFields = [displayedWeirdFields weirdfields{iprint} ' '];
    end
    error([mfilename ':UnrecognisedFields'], ...
          ['The following oil.ica settings were not recognized ', ...
           'by osl_check_oil: \n ' displayedWeirdFields '\n']);
end % if ~isempty(weirdfields)

weirdfields = fieldnames(oilin.ica_first_level);
if ~isempty(weirdfields)
    displayedWeirdFields = [];
    for iprint = 1:numel(weirdfields)
        displayedWeirdFields = [displayedWeirdFields weirdfields{iprint} ' '];
    end
    error([mfilename ':UnrecognisedFields'], ...
          ['The following oil.ica_first_level settings were not recognized ', ...
           'by osl_check_oil: \n ' displayedWeirdFields '\n']);
end % if ~isempty(weirdfields)

weirdfields = fieldnames(oilin.ica_group_level);
if ~isempty(weirdfields)
    displayedWeirdFields = [];
    for iprint = 1:numel(weirdfields)
        displayedWeirdFields = [displayedWeirdFields weirdfields{iprint} ' '];
    end
    error([mfilename ':UnrecognisedFields'], ...
          ['The following oil.ica_group_level settings were not recognized ', ...
           'by osl_check_oil: \n ' displayedWeirdFields '\n']);
end % if ~isempty(weirdfields)

oilin = rmfield(oilin,'source_recon');
oilin = rmfield(oilin,'enveloping');
oilin = rmfield(oilin,'concat_subs');
oilin = rmfield(oilin,'ica');
oilin = rmfield(oilin,'ica_first_level');
oilin = rmfield(oilin,'ica_group_level');

try 
    oilin = rmfield(oilin,'fname'); 
catch
    % do nothing
end;

weirdfields = fieldnames(oilin);
if ~isempty(weirdfields)
    displayedWeirdFields = [];
    for iprint = 1:numel(weirdfields)
        displayedWeirdFields = [displayedWeirdFields weirdfields{iprint} ' '];
    end
    error([mfilename ':UnrecognisedFields'], ...
          ['The following oil settings were not recognized ', ...
           'by osl_check_oil: \n ' displayedWeirdFields '\n']);
end % if ~isempty(weirdfields)
